const md5 = require('md5');
const rp = require('request-promise');

module.exports = class extends think.Service {
  async open(index = '1') {
    try {
      const options = {
        method: 'POST',
        uri: 'http://47.96.250.195:8901/venus_api_download_uri',
        body: {
          sn: ['2509800'],
          table: 'TabCommand',
          cmd: 'remote_open_door',
          data: [
            {
              openDoor: index,
              AudioName: '1.wav',
              showInfo: '欢迎光临',
            },
          ],
        },
        json: true,
      };

      let sessionData = await rp(options);
      console.log(sessionData, 1231);
      return sessionData;
    } catch (e) {
      return null;
    }
  }

  async getToken() {
    try {
      const timestamp = Date.now().toString();
      const tokenOptions = {
        method: 'POST',
        uri: 'http://47.96.250.195:8902/exterior/api/getToken?userName=admin',
        body: {
          timestamp: timestamp,
        },
        json: true,
      };
      let sessionToken = await rp(tokenOptions);
      return {
        token: sessionToken.data.token,
        timestamp: timestamp,
      };
    } catch (e) {
      return null;
    }
  }

  async getUser(id) {
    try {
      const token = await this.getToken();
      console.log(token, 'token');
      const userOptions = {
        method: 'POST',
        uri: 'http://47.96.250.195:8902/exterior/api/operation',
        headers: token,
        body: { inType: 'perm', opType: 'queryuser', data: { permId: id } },
        json: true,
      };
      let sessionUser = await rp(userOptions);
      console.log(sessionUser, 'sessionUser');
      return sessionUser.data;
    } catch (e) {
      console.log(e, 'e');
      return null;
    }
  }

  async shatdown(list) {
    try {
      const token = await this.getToken();
      console.log(token, 'token');
      const userOptions = {
        method: 'POST',
        uri: 'http://47.96.250.195:8902/exterior/api/operation',
        headers: token,
        body: {
          inType: 'user',
          opType: 'update',
          data: list,
        },
        json: true,
      };
      let sessionUser = await rp(userOptions);
      console.log(sessionUser, 'sessionUser');
      return sessionUser.data;
    } catch (e) {
      console.log(e, 'e');
      return null;
    }
  }

  async holiday() {
    try {
      const options = {
        method: 'POST',
        uri: 'http://47.96.250.195:8901/venus_api_download_uri',
        body: {
          sn: ['SIWO202310178888'],
          table: 'TabHoliday',
          cmd: 'update',
          msgId: '1644',
          callBackUrl: 'http://47.96.250.195:8080/api/aicom_log',
          data: [
            {
              HolidayPlanID: '122',
              Date: '2025-03-19',
              TimeStart: '08:30',
              TimeEnd: '19:30',
            },
          ],
        },
        json: true,
      };

      let sessionData = await rp(options);
      console.log(sessionData, 1222231);
      return sessionData;
    } catch (e) {
      return null;
    }
  }
};
